package com.ruoyi.project.manhua.page.service.impl;

import java.util.Arrays;
import java.util.List;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.qiniu.QiNiuYunUtils;
import com.ruoyi.project.manhua.mistake.service.IMhClimbMistakeService;
import com.ruoyi.project.manhua.newXml.MhPageNewMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.manhua.page.domain.MhPage;
import com.ruoyi.project.manhua.page.service.IMhPageService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 漫画篇章页数Service业务层处理
 *
 * @author yuan
 * @date 2021-01-19
 */
@Service
public class MhPageServiceImpl implements IMhPageService {
    @Autowired
    private MhPageNewMapper mhPageMapper;

    @Autowired
    private QiNiuYunUtils qiNiuYunUtils;

    @Autowired
    private IMhClimbMistakeService mhClimbMistakeService;

    /**
     * 查询漫画篇章页数
     *
     * @param id 漫画篇章页数ID
     * @return 漫画篇章页数
     */
    @Override
    public MhPage selectMhPageById(Long id) {
        return mhPageMapper.selectMhPageById(id);
    }

    /**
     * 查询漫画篇章页数列表
     *
     * @param mhPage 漫画篇章页数
     * @return 漫画篇章页数
     */
    @Override
    public List<MhPage> selectMhPageList(MhPage mhPage) {
        return mhPageMapper.selectMhPageList(mhPage);
    }

    /**
     * 新增漫画篇章页数
     *
     * @param mhPage 漫画篇章页数
     * @return 结果
     */
    @Override
    public int insertMhPage(MhPage mhPage) {
        mhPage.setCreateTime(DateUtils.getNowDate());
        return mhPageMapper.insertMhPage(mhPage);
    }

    /**
     * 修改漫画篇章页数
     *
     * @param mhPage 漫画篇章页数
     * @return 结果
     */
    @Override
    public int updateMhPage(MhPage mhPage) {
        mhPage.setUpdateTime(DateUtils.getNowDate());
        return mhPageMapper.updateMhPage(mhPage);
    }

    /**
     * 删除漫画篇章页数对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteMhPageByIds(String ids) {
        String[] array = Convert.toStrArray(ids);

        /*删除七牛云图片*/
        Arrays.stream(array).forEach((e) -> {
            this.deleteMhPageById(Long.valueOf(e));
        });
        return 1;
        // return mhPageMapper.deleteMhPageByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除漫画篇章页数信息
     *
     * @param id 漫画篇章页数ID
     * @return 结果
     */
    @Override
    public int deleteMhPageById(Long id) {

        MhPage mhPage = mhPageMapper.selectMhPageById(id);
        if (mhPage.getSrc() != null && !"".equals(mhPage.getSrc()) && mhPage.getSrc().indexOf("http") == -1) {
            Integer delete = qiNiuYunUtils.getDelete(mhPage.getSrc());
            if (delete != 200) {
                throw new BusinessException("删除七牛云图片失败...");
            }
        }
        mhClimbMistakeService.deleteMhClimbMistakeByPageId(id);

        return mhPageMapper.deleteMhPageById(id);
    }


    /**
     * @param nameId    漫画id
     * @param chapterId 漫画篇章id
     * @param order     升序/降序
     * @return
     */
    @Override
    public List<MhPage> selectMhPageListOrder(Long nameId, Long chapterId, String order) {
        /*没有传值 默认升序*/
        if (order.equals(Constants.mysqlOrder.asc)) {
            System.err.println(Constants.mysqlOrder.asc);
            order = Constants.mysqlOrder.asc;
        } else {
            order = Constants.mysqlOrder.desc;
            System.err.println(Constants.mysqlOrder.desc);
        }
        return mhPageMapper.selectMhPageListOrder(nameId, chapterId, order);
    }
}
